//
//  YATableViewCell.swift
//  KeepCliming
//
//  Created by 鱼龙之梦 on 15/9/6.
//  Copyright © 2015年 panyanzhi. All rights reserved.
//

import UIKit
let UIScreenWidth = UIScreen.mainScreen().bounds.width;
let YAStatusCellMargin:CGFloat = 12
let YAStatusIconWidth:CGFloat = 35
let YAStatusPicMargin:CGFloat = 5
//改了这里可以多张图片的情况居中
let YAStatusPicWidth:CGFloat = (UIScreenWidth - 7 * YAStatusPicMargin)/3
//MARK: 定义一个点击URL的代理
protocol YAStatusCellDelegate:NSObjectProtocol{
    func statusCellDidClickURL(url:NSURL)
}
class YAStatusCell: UITableViewCell {
    weak var cellDelegate:YAStatusCellDelegate?
    //MARK:--- 加入微博数据视图模型
    private lazy var topView:YAStatusTopView = YAStatusTopView()
    lazy var bottomView:YAStatusBottomView = YAStatusBottomView()
    lazy var contentLabel :FFLabel = FFLabel(title: nil, color: UIColor.darkGrayColor(), fontSize: 15, layoutWidth: UIScreenWidth-2*YAStatusCellMargin)
    var statusVM:YAStatusVM?{
        didSet{
            topView.statusVM = statusVM
            //微博正文
            let statusText = statusVM?.status.text ?? ""
            contentLabel.attributedText = YAEmoticonViewModel.sharedEVM.emoticonText(statusText, font: contentLabel.font)
            //设置配图视图数据，内部将完成视图size的设置
            pictureView.statusViewModel = statusVM
            picHeightCons?.constant = pictureView.bounds.height
            picWidthCons?.constant = pictureView.bounds.width
            picTopCons?.constant = (pictureView.bounds.width==0) ? 0 :YAStatusPicMargin
            //  pic3MarginCons?.constant = pictureView.contentInset
        
            
        }
    }
    //配图视图宽高约束以及顶部约束
    var picWidthCons:NSLayoutConstraint?
    var picHeightCons:NSLayoutConstraint?
    var picTopCons:NSLayoutConstraint?
    var pic3MarginCons:NSLayoutConstraint?
    
    //MARK:--- 计算行高
    func rowHeight(viewModel:YAStatusVM)->CGFloat{
        statusVM = viewModel
        layoutIfNeeded()
        return CGRectGetMaxY(bottomView.frame)
    }
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.setupUI()
        pictureView.scrollsToTop = false
    }
    
    func setupUI(){
        contentView.addSubview(topView)
        //分割视图
        let topSepView = UIView()
        topSepView.backgroundColor = UIColor(white: 0.85, alpha: 1.0)
        contentView.addSubview(topSepView)
        contentView.addSubview(contentLabel)
        contentView.addSubview(pictureView)
        contentView.addSubview(bottomView)
        setupUIConstraint(topSepView)
        contentLabel.labelDelegate = self
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    private func setupUIConstraint(topSepView:UIView){
        topSepView.ff_AlignInner(type: ff_AlignType.TopLeft, referView: contentView, size: CGSize(width:UIScreenWidth, height: 10))
        topView.ff_AlignInner(type: ff_AlignType.TopLeft, referView: contentView, size: CGSize(width: UIScreenWidth, height: YAStatusCellMargin+YAStatusIconWidth),offset:CGPoint(x: 0, y: 10))
        
        contentLabel.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: topView, size: nil, offset: CGPoint(x: YAStatusCellMargin, y: YAStatusCellMargin))
        
        //配图约束：剪切到NormalCell中了
        // 5> 底部视图
        bottomView.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: pictureView, size: CGSize(width: UIScreenWidth, height: 44), offset: CGPoint(x: -YAStatusCellMargin, y: YAStatusCellMargin))
        //指定底部视图相对底边的约束
        // bottomView.ff_AlignInner(type: ff_AlignType.BottomRight, referView: contentView, size: nil)
    }
    lazy var pictureView:YAStatusPictureView = YAStatusPictureView()
}
//MARK:-- FF标签的代理
extension YAStatusCell:FFLabelDelegate{
    func labelDidSelectedLinkText(label:FFLabel, text: String) {
        print(text)
        if !text.hasPrefix("http://"){
            return
        }
        guard let url = NSURL(string:text) else {
            return
        }
        cellDelegate?.statusCellDidClickURL(url)
    }
}